package com.xiaoniu.forum.service.impl;

import com.xiaoniu.forum.dao.CommunityDao;
import com.xiaoniu.forum.entity.CommunityEntity;
import com.xiaoniu.forum.entity.vo.CommunityTopicOptionVo;
import com.xiaoniu.forum.service.CommunityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaoniu.forum.common.utils.PageUtils;
import com.xiaoniu.forum.common.utils.Query;

import com.xiaoniu.forum.dao.CommunityTopicCategoryDao;
import com.xiaoniu.forum.entity.CommunityTopicCategoryEntity;
import com.xiaoniu.forum.service.CommunityTopicCategoryService;
import org.springframework.util.StringUtils;


@Service("communityTopicCategoryService")
public class CommunityTopicCategoryServiceImpl extends ServiceImpl<CommunityTopicCategoryDao, CommunityTopicCategoryEntity> implements CommunityTopicCategoryService {

    @Autowired
    private CommunityDao communityDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        QueryWrapper<CommunityTopicCategoryEntity> wrapper = new QueryWrapper<>();

        String communityId = (String) params.get("communityId");
        if (!StringUtils.isEmpty(communityId)){
            wrapper.eq("community_id",communityId);
        }

        IPage<CommunityTopicCategoryEntity> page = this.page(
                new Query<CommunityTopicCategoryEntity>().getPage(params),
                wrapper
        );

        List<CommunityEntity> communitys = communityDao.selectList(null);

        for (CommunityTopicCategoryEntity topicCategoryEntity : page.getRecords()) {
            for (CommunityEntity community : communitys) {
                if (topicCategoryEntity.getCommunityId().equals(community.getId())){
                    topicCategoryEntity.setCommunityName(community.getName());
                }
            }
        }

        return new PageUtils(page);
    }

    @Override
    public List<CommunityTopicOptionVo> getAllWithOptionsByCommunityId(String communityId) {

        List<CommunityTopicOptionVo> list = new ArrayList();

        QueryWrapper<CommunityTopicCategoryEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("community_id",communityId);

        List<CommunityTopicCategoryEntity> communityTopicCategoryEntityList = baseMapper.selectList(wrapper);

        int index = 0;

        for (CommunityTopicCategoryEntity communityTopicCategoryEntity : communityTopicCategoryEntityList) {
            CommunityTopicOptionVo communityTopicOptionVo = new CommunityTopicOptionVo(communityTopicCategoryEntity.getId(), communityTopicCategoryEntity.getName(), index++);
            //最终节点 所以不需要有子节点了
            communityTopicOptionVo.setChildren(null);
            list.add(communityTopicOptionVo);
        }

        return list;

    }


}